package com.xingkeduo.business.order.service;

import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.dto.HairstyleDetailResponseDTO;
import com.xingkeduo.dto.HairstyleTableDTO;
import com.xingkeduo.dto.SeeShopIdVo;
import com.xingkeduo.dto.param.HairstyleDetailParameterDto;
import com.xingkeduo.dto.param.HairstyleSelectedParameterDto;
import com.xingkeduo.dto.param.ShopsParameterDto;
import com.xingkeduo.dto.transter.hairstyleDetails.*;
import com.xingkeduo.dto.transter.hairstyleSelected.ContentHairstyleSelected;
import com.xingkeduo.dto.transter.hairstyleSelected.HairstyleList;
import com.xingkeduo.dto.transter.hairstyleSelected.HairstyleSelect;
import com.xingkeduo.dto.transter.shopDistance.DistanceContent;
import com.xingkeduo.dto.transter.shopDistance.DistanceReturn;
import com.xingkeduo.json.BaseDTOJTO;
import com.xingkeduo.remote.feign.AdminFeignService;
import com.xingkeduo.remote.feign.StoreFeignService;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.utils.ResponseUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OperateService {

    @Resource
    private StoreFeignService storeFeignService;
    @Resource
    private AdminFeignService adminFeignService;

    /**
     * 获取店铺列表 -- 小程序
     *
     * @param shopsParameterDto
     * @return
     */
    public String store_list(ShopsParameterDto shopsParameterDto) {
        try {
        	
//        	if(shopsParameterDto!=null && shopsParameterDto.getF_user_id()!=null && shopsParameterDto.getF_user_id().longValue() > 0L) {
//        		ResponseUtils<Long> responseUtils = adminFeignService.checkUserExist(shopsParameterDto.getF_user_id(),shopsParameterDto.getMobile_type());
//                if (!responseUtils.success()) {
//                    return BaseDTOJTO.toUserAuthError(responseUtils.getCode()).toString();
//                }
//        	}
        	
            return storeFeignService.list_transfer(shopsParameterDto.getF_user_id(), shopsParameterDto.getLongitude(), shopsParameterDto.getLatitude(), shopsParameterDto.getStatus(), shopsParameterDto.getOffset(), shopsParameterDto.getIs_first(), shopsParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 获取店铺详情 -- 小程序
     *
     * @param shopsParameterDto
     * @return
     */
    public String store_detail(ShopsParameterDto shopsParameterDto) {
        //先校验user_id是否传值  没传值则返回9004
//        if (shopsParameterDto.getF_user_id() == null || shopsParameterDto.getF_user_id().longValue() <= 0L) {
//            return BaseDTOJTO.toUserAuthError("9004").toString();
//        }
        try {
            //再校验user_id对应的用户是否存在  不存在则返回9004
//            if (shopsParameterDto.getF_user_id().longValue() > 0L) {//调用子峰接口 进行判断
//                ResponseUtils<Long> responseUtils = adminFeignService.checkUserExist(shopsParameterDto.getF_user_id(),shopsParameterDto.getMobile_type());
//                if (!responseUtils.success()) {
//                    return BaseDTOJTO.toUserAuthError(responseUtils.getCode()).toString();
//                }
//            }
            return storeFeignService.detail_transfer(shopsParameterDto.getF_user_id(), shopsParameterDto.getLongitude(), shopsParameterDto.getLatitude(), shopsParameterDto.getStore_id(), shopsParameterDto.getTime(), shopsParameterDto.getToken(), shopsParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 获取发型精选列表
     *
     * @param
     * @return
     */
    public HairstyleSelect getHairstyleList(HairstyleSelectedParameterDto hairstyleSelectedParameterDto) {

        HairstyleQueryDTO hairstyleQueryDTO = new HairstyleQueryDTO();
        hairstyleQueryDTO.cover(hairstyleSelectedParameterDto);
        ResponseUtils<Page<HairstyleTableDTO>> listResponseUtils = storeFeignService.doGetHairLists(JSONObject.toJSONString(hairstyleQueryDTO));

        HairstyleSelect hairstyleSelect = new HairstyleSelect();

        if (listResponseUtils.success()) {

            List<HairstyleTableDTO> listResponseUtilsData = listResponseUtils.getData().getDtoList();
            List<HairstyleList> hairstyleLists = listResponseUtilsData.stream()
                    .map(this::queryOrders)
                    .collect(Collectors.toList());
            ContentHairstyleSelected contentHairstyleSelected = new ContentHairstyleSelected();
            contentHairstyleSelected.covert(hairstyleLists);

            hairstyleSelect.covert(listResponseUtils, contentHairstyleSelected);

        }
        return hairstyleSelect;
    }

    private HairstyleList queryOrders(HairstyleTableDTO hairstyleTableDTO) {
        HairstyleList hairstyleList = new HairstyleList();
        hairstyleList.covert(hairstyleTableDTO);
        return hairstyleList;
    }


    /**
     * 获取发型详情
     *
     * @param
     * @return
     */
    public HairstyleDetail getHairstyleDetail(HairstyleDetailParameterDto hairstyleDetailParameterDto) {
        HairstyleDetail hairstyleDetail = new HairstyleDetail();
        hairstyleDetailParameterDto.cover(hairstyleDetailParameterDto);

        ResponseUtils<HairstyleDetailResponseDTO> hairstyleResponseDTOResponseUtils = storeFeignService.doGetHairstyleDetail(JSONObject.toJSONString(hairstyleDetailParameterDto));
        if (hairstyleResponseDTOResponseUtils.success()) {
            HairstyleDetailResponseDTO hairstyleDetailResponseDTO = hairstyleResponseDTOResponseUtils.getData();

            Hair hair = new Hair();
            hair.cover(hairstyleDetailResponseDTO);
            HairstyleQueryDTO hairstyleQueryDTO = new HairstyleQueryDTO();
            ResponseUtils<Page<HairstyleTableDTO>> listResponseUtils = storeFeignService.doGetHairLists(JSONObject.toJSONString(hairstyleQueryDTO));
//
            if (listResponseUtils.success()) {
                // SuggestList suggestList = new SuggestList();
                List<HairstyleTableDTO> listResponseUtilsData = listResponseUtils.getData().getDtoList();
                List<SuggestList> suggestList = listResponseUtilsData.stream()
                        .map(this::queryHairstyleDetail)
                        .collect(Collectors.toList());


                ContentHairstyleDetail contentHairstyleDetail = new ContentHairstyleDetail();
                contentHairstyleDetail.cover(hair, suggestList);

                hairstyleDetail.cover(contentHairstyleDetail, hairstyleResponseDTOResponseUtils);
            }
        }
        return hairstyleDetail;
    }

    private SuggestList queryHairstyleDetail(HairstyleTableDTO hairstyleTableDTO) {
        SuggestList suggestList = new SuggestList();
        suggestList.cover(hairstyleTableDTO);
        return suggestList;
    }


    public DistanceReturn getLongitudeAndLatitudeShopInfo(Double latitude, Double longitude, Long f_user_id) {

        ResponseUtils<SeeShopIdVo> longitudeAndLatitudeShopInfo = storeFeignService.getLongitudeAndLatitudeShopInfo(latitude, longitude, f_user_id);
        DistanceReturn distanceReturn = new DistanceReturn();
        if (longitudeAndLatitudeShopInfo.success()) {
            SeeShopIdVo data = longitudeAndLatitudeShopInfo.getData();
            DistanceContent distanceContent = new DistanceContent();
            distanceContent.cover(data);
            distanceReturn.cover(longitudeAndLatitudeShopInfo, distanceContent);
        }
        return distanceReturn;
    }

}
